package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.EnterpriseProcurementAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;

/**
 * 企业自采属性联动处理类 field_name:200003676
 */
@Service
@AttrHandler(EnterpriseProcurementAttrHandler.class)
public class EnterpriseProcurementAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //SIM卡类型
        KiteAttr simCardTypeAttr = inst.getAttr("200003678");
        if (Objects.isNull(simCardTypeAttr)) {
            return;
        }
        List<KiteAttrValue> kiteAttrValues = new ArrayList<>();
        List<AttrValue> attrValues = new ArrayList<>();
        // 1_0000107 否
        if (KiteStringUtils.equals("1_0000107", attr.getValue())) {
            attrValues = AttrValueCache.get("2020000006");
        }
        else {
            attrValues = AttrValueCache.get("2020000007");
        }
        if (KiteListUtils.isNotEmpty(attrValues)) {
            attrValues.stream().forEach(attrValue -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(attrValue.getAttrValue());
                kiteAttrValue.setAttrValueName(attrValue.getAttrValueName());
                kiteAttrValues.add(kiteAttrValue);
            });
            simCardTypeAttr.setAttrValues(kiteAttrValues);
        }

        if (attrValues.stream().anyMatch(
            attrValue -> KiteStringUtils.isEqual(attrValue.getAttrValue(), simCardTypeAttr.getDefaultValue()))) {
            operateInstAttrService
                .setAttrValue(inst, "200003678", simCardTypeAttr.getDefaultValue(), data.getSceneInstId(), null);
        }
        else {
            operateInstAttrService
                .setAttrValue(inst, "200003678", "", data.getSceneInstId(), null);
        }
        // 是
        if (KiteStringUtils.isEqual("1_0000106", attr.getValue())) {
            LocalAttrHelper.showAndNonnull(inst, "200004040");
        }
        else if (KiteStringUtils.isEqual("1_0000107", attr.getValue())) {
            LocalAttrHelper.hideAndNullable(inst, "200004040");
        }
//        operateSceneInstService
//            .setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), simCardTypeAttr);
//        operateInstAttrService
//            .setAttrValue(inst, "200003678", simCardTypeAttr.getDefaultValue(), data.getSceneInstId(), null);
//        LocalAttrHelper.hideAndNullable(inst, "200004040");
//        LocalAttrHelper.hideAndNullable(inst, "200004041");
//        LocalAttrHelper.hideAndNullable(inst, "200004042");
//        LocalAttrHelper.hideAndNullable(inst, "200004043");
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
